//
//  UserMessageView.swift
//  swiftProject
//
//  Created by ZhangSai on 2021/6/10.
//

import UIKit

protocol UserMessageDelegate:class {
    func homeViewAcrionDelegate(actonType:HomeViewActonType)
}

class UserMessageView: UIView {
    weak var delegate:UserMessageDelegate?

    @IBOutlet weak var useHeadBtn: UIButton!
    @IBOutlet weak var burmaBtn: UIButton!
    @IBOutlet weak var americeBtn: UIButton!
    @IBOutlet weak var chinaBtn: UIButton!
    @IBOutlet weak var burmaLab: UILabel!
    @IBOutlet weak var americeLab: UILabel!
    @IBOutlet weak var chinaLab: UILabel!
    @IBOutlet weak var nameLab: UILabel!
    @IBOutlet weak var messageLab: UILabel!
    @IBOutlet weak var settintBtn: UIButton!

    @IBOutlet weak var addressView: UIView!
    @IBOutlet weak var addressNewView: UIView!
    @IBOutlet weak var lookExpressLab: UILabel!
    @IBOutlet weak var addressLab: UILabel!
    @IBOutlet weak var senderView: UIView!
    @IBOutlet weak var senderOrderLab: UILabel!
    @IBOutlet weak var senderOrderMsgLab: UILabel!
    
    var selectBtn:UIButton!
    
    override func awakeFromNib() {
        super.awakeFromNib()
        self.addressView.isUserInteractionEnabled = true
        self.addressNewView.isUserInteractionEnabled = true
        self.senderView.layer.cornerRadius = 7.5
        self.senderView.layer.shadowColor = UIColor.init(red: 0/255.0, green: 0/255.0, blue: 0/255.0, alpha: 0.2).cgColor
        self.senderView.layer.shadowOffset = CGSize.init(width: 0, height: 1) //投影偏移
        self.senderView.layer.shadowOpacity = 1
        self.senderView.layer.shadowRadius = 2
        
        self.addressView.layer.cornerRadius = 7.5
        self.addressView.layer.shadowColor = UIColor.init(red: 0/255.0, green: 0/255.0, blue: 0/255.0, alpha: 0.2).cgColor
        self.addressView.layer.shadowOffset = CGSize.init(width: 0, height: 1)//投影偏移
        self.addressView.layer.shadowOpacity = 1
        self.addressView.layer.shadowRadius = 2
        
        self.addressNewView.layer.cornerRadius = 7.5
        self.addressNewView.layer.shadowColor = UIColor.init(red: 0/255.0, green: 0/255.0, blue: 0/255.0, alpha: 0.2).cgColor
        self.addressNewView.layer.shadowOffset = CGSize.init(width: 0, height: 1)//投影偏移
        self.addressNewView.layer.shadowOpacity = 1;
        self.addressNewView.layer.shadowRadius = 2;
  
        self.senderView.isUserInteractionEnabled = true;
        
        let tapGR = UITapGestureRecognizer.init(target: self, action: #selector(senderOrderAction))
        self.senderView.addGestureRecognizer(tapGR)
        let addressTap = UITapGestureRecognizer.init(target: self, action: #selector(senderOrderAction))
        self.addressNewView.addGestureRecognizer(addressTap)
        let lookExpressTap = UITapGestureRecognizer.init(target: self, action: #selector(senderOrderAction))
        self.addressView.addGestureRecognizer(lookExpressTap)
        
        let str = UserDefaults.standard.string(forKey: LanguageHeader.MySystemLanguage)
        self.americeLab.textColor = UIColor.init(hexString: "#565656")
        self.burmaLab.textColor = UIColor.init(hexString: "#565656")
        self.chinaLab.textColor = UIColor.init(hexString: "#565656")
        if (str == LanguageHeader.ChinaLanguage) {
            self.chinaLab.textColor = UIColor.black
            self.selectBtn = self.chinaBtn
        } else if (str == LanguageHeader.BurmaLanguage) {
            self.burmaLab.textColor = UIColor.black
            self.selectBtn = self.burmaBtn
        } else {
            self.americeLab.textColor = UIColor.black
            self.selectBtn = self.americeBtn
        }
        self.selectBtn.isSelected = true
        self.nameLab.text = WLUserModel.getAvatarPath()
        self.burmaLab.isUserInteractionEnabled = true
        self.americeLab.isUserInteractionEnabled = true
        self.chinaLab.isUserInteractionEnabled = true

        let tapBurmaLab = UITapGestureRecognizer.init(target: self, action: #selector(burmaLabTap))
        self.burmaLab.addGestureRecognizer(tapBurmaLab)
        let tapAmerice = UITapGestureRecognizer.init(target: self, action: #selector(americeTap))
        self.americeLab.addGestureRecognizer(tapAmerice)
        let tapChina = UITapGestureRecognizer.init(target: self, action: #selector(chinaTap))
        self.chinaLab.addGestureRecognizer(tapChina)
        
        self.setUI()
    }
    
    func setUI() {
        self.lookExpressLab.text = ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_lookExpress)
        self.addressLab.text = ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_addressBook)
        self.senderOrderLab.text = ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_senderExpress)
        self.senderOrderMsgLab.text = ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_senderOrderMsg)
        self.nameLab.text = "Hi " + WLUserModel.getPhone()
        self.messageLab.text = ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_wepozt_serve)
        self.useHeadBtn.layer.cornerRadius = self.useHeadBtn.frame.size.width/2
    }
    
    @objc func burmaLabTap() {
        self.clickCountryBtn(self.burmaBtn)
    }
    
    @objc func americeTap() {
        self.clickCountryBtn(self.americeBtn)
    }
    
    @objc func chinaTap() {
        self.clickCountryBtn(self.chinaBtn)
    }
    
    @IBAction func clickCountryBtn(_ sender: UIButton) {
        if self.selectBtn == sender {
            return
        }
        self.selectBtn.isSelected = false
        self.americeLab.textColor = UIColor.init(hexString: "#565656")
        self.burmaLab.textColor = UIColor.init(hexString: "#565656")
        self.chinaLab.textColor = UIColor.init(hexString: "#565656")
        if sender.tag == 100 {
            self.burmaLab.textColor = UIColor.black
            self.selectBtn = self.burmaBtn;
            UserDefaults.standard.setValue(LanguageHeader.BurmaLanguage, forKey: LanguageHeader.MySystemLanguage)
        } else if sender.tag == 101 {
            self.americeLab.textColor = UIColor.black
            self.selectBtn = self.americeBtn
            UserDefaults.standard.setValue(LanguageHeader.USALanguage, forKey: LanguageHeader.MySystemLanguage)
        } else {
            self.chinaLab.textColor = UIColor.black
            self.selectBtn = self.chinaBtn
            UserDefaults.standard.setValue(LanguageHeader.ChinaLanguage, forKey: LanguageHeader.MySystemLanguage)
        }
        self.selectBtn.isSelected = true
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: "languageNotification"), object: self)
        self.setUI()
    }

    @IBAction func clickSetting(_ sender: UIButton) {
        self.delegate?.homeViewAcrionDelegate(actonType: HomeViewActonType.Home_SettingType)
    }
    
    @IBAction func clickHeadBtn(_ sender: UIButton) {
        self.delegate?.homeViewAcrionDelegate(actonType: HomeViewActonType.Home_HeadPortraitType)
    }
    
    @objc func senderOrderAction() {
        self.delegate?.homeViewAcrionDelegate(actonType: HomeViewActonType.Home_SenderOrderType)
    }
    func addressAction() {
        self.delegate?.homeViewAcrionDelegate(actonType: HomeViewActonType.Home_AddressBookType)
    }
    func lookExpressAction() {
        self.delegate?.homeViewAcrionDelegate(actonType: HomeViewActonType.Home_LookOrderTrackType)
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesBegan(touches, with: event)
        self.delegate?.homeViewAcrionDelegate(actonType: HomeViewActonType.Home_EndEditingType)
    }
}
